package com.java.yz.redis_jdbc;

import com.java.yz.mysql_jdbc.MySQLTool;
import redis.clients.jedis.Jedis;

import java.sql.*;
import java.util.List;
import java.util.Scanner;
import java.util.UUID;

//redis当做缓存，储存热点数据
public class redisApiDemo2 {
    static Connection connection = null;
    static Statement statement = null;
    static Scanner scanner = new Scanner(System.in);
    static Jedis jedis = null;

    public static void main(String[] args) {
        //加载驱动
        try {
            //获取与redis的连接对象
            jedis = new Jedis("192.168.147.100", 6379);

            //以后就可以使用工具类的方式来获取
            Connection connection = MySQLTool.getPreparedStatement();
            //获取操作数据库对象
            statement = connection.createStatement();

            //获取能够防止sql注入问题的数据库操作对象

            PreparedStatement preparedStatement = connection.prepareStatement("select username,password from login where username=? and password=?");


            System.out.println("===============================欢迎来到yz商城网===============================");
            System.out.print("欢迎登录！请输入用户名：");
            String name = scanner.nextLine();
            System.out.print("请输入密码：");
            String pass = scanner.nextLine();

            //编写sql语句，看是否有该用户
//            String sql = "select username,password from login where username='" + name + "' and password='" + pass + "'";
            //"select username,password from login where username='" + yangz + "' and password='" + 123 ' or '1 '='1 + "'";
            //123 ' or '1 '='1   两个都是true，肯定可以进去


            //设置sql的值，？代表占位符
            preparedStatement.setString(1, name);
            preparedStatement.setString(2, pass);
            //这样就整体当作一个值了


            //执行查询sql语句
            ResultSet resultSet = preparedStatement.executeQuery();
            //System.out.println(resultSet.getFetchSize());   //返回的是0，说明里面没有数据
            if (!resultSet.next()) {
                System.out.println("对不起！该用户未注册，请先注册！");
                System.out.println("是否进行注册？ Y/N");
                String s = scanner.next();
                if ("Y".equals(s)) {
                    zhuce();
                } else if ("N".equals(s)) {
                    System.out.println("欢迎下次使用");
                }
            } else {
                System.out.println("登录成功！>_^，请挑选商品");
                System.out.println("请输入商品的名称：");
                String goodsName = scanner.next();

                //先从redis数据库中去查询
                List<String> lrange = jedis.lrange(goodsName, 0, -1);
                if (lrange.size() == 0) {
                    System.out.println("=============================这是直接走mysql查询=============================");
                    //redis没有该类商品，就应该去mysql中查询
                    PreparedStatement prep1 = connection.prepareStatement("select * from jd_goods where name like ?");
                    prep1.setString(1, "%" + goodsName + "%");
                    ResultSet resultSet1 = prep1.executeQuery();
                    while (resultSet1.next()) {
                        StringBuilder stringBuilder = new StringBuilder();
                        String id = resultSet1.getString(1);
                        stringBuilder.append(id);
                        stringBuilder.append(" | ");
                        String goodsname = resultSet1.getString(2);
                        stringBuilder.append(goodsname);
                        stringBuilder.append(" | ");
                        String prices = resultSet1.getString(3);
                        stringBuilder.append(prices);
                        stringBuilder.append(" | ");
                        String shop = resultSet1.getString(4);
                        stringBuilder.append(shop);
                        stringBuilder.append(" | ");
                        String flags = resultSet1.getString(5);
                        stringBuilder.append(flags);
                        stringBuilder.append(" | ");
                        String area = resultSet1.getString(6);
                        stringBuilder.append(area);
                        stringBuilder.append(" | ");
                        String cpu = resultSet1.getString(7);
                        stringBuilder.append(cpu);
                        stringBuilder.append(" | ");
                        String size = resultSet1.getString(8);
                        stringBuilder.append(size);
                        stringBuilder.append(" | ");
                        String memorySize = resultSet1.getString(9);
                        stringBuilder.append(memorySize);
                        stringBuilder.append(" | ");
                        String gpu = resultSet1.getString(10);
                        stringBuilder.append(gpu);
                        stringBuilder.append(" | ");
                        String comments = resultSet1.getString(11);
                        stringBuilder.append(comments);
                        stringBuilder.append(" | ");
                        String s = stringBuilder.toString();
                        System.out.println(s);

                        //将mysql查到的数据向redis中写一份
                        jedis.lpush(goodsName,s);
                        //设置过期时间
                        jedis.expire(goodsName,120);
                    }
                }
                else {
                    System.out.println("=============================这是直接走redis查询=============================");
                    //redis中包含该商品
                    for (String s : lrange) {
                        System.out.println(s);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (jedis != null) {
                jedis.close();
            }
        }
    }

    //在这里注册
    public static void zhuce() {
        System.out.println("欢迎注册yz商城网");
        System.out.print("请输入新用户名：");
        String next = scanner.next();
        System.out.print("请输入新密码：");
        String next1 = scanner.next();
        System.out.println("注册中，请稍等.....^_^");

        //id随机且唯一
        //java提供了一个UUID，随机生成一个id
        String id = UUID.randomUUID().toString();

        String sql = "insert into login value('" + id + "','" + next + "','" + next1 + "')";
        try {
            int i = statement.executeUpdate(sql);
            Thread.sleep(3000);
            if (i == 1) {
                System.out.println("用户注册成功！");
            } else {
                System.out.println("新用户注册失败！请一小时后来！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
